/*
 * DateTimeConverter.java
 * 
 * Created on Sep 4, 2007, 10:50:34 AM
 * 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.vce.web.jsf;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

/**
 *
 * @author Lruiz
 */
public class DateTimeConverter implements Converter{
    private String pattern = "MM/dd/yyyy hh:mm a";
    private SimpleDateFormat format; 
    
    public DateTimeConverter() {
    }
    
    public void init(){
        if(format == null){
            format = new SimpleDateFormat(pattern);
        }
    }

    public Object getAsObject(FacesContext context, UIComponent comp, String value) {
        try {
            init();
            return format.parse(value);
        } catch (ParseException ex) {
        }
        return null;
    }

    public String getAsString(FacesContext context, UIComponent comp, Object value) {
        init();
        return format.format((Date)value);
    }

}
